using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entities;
using System.Configuration;

namespace DAL
{
   public  class tbSanPhamDAL
    {
       private SqlConnection cn = null;
        public tbSanPhamDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbSanPhamDAL(tbSanPhamDAL dao)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
       public void Insert(tbSanPham sp)
       {
           SqlCommand cm = new SqlCommand("tbSanPham_Insert", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaDN", sp.MaDN);
           cm.Parameters.AddWithValue("@TenSP", sp.TenSP);
           cm.Parameters.AddWithValue("@ChietKhau", sp.ChietKhau);
           cm.Parameters.AddWithValue("@ChiTietSP", sp.ChiTietSP);
           cm.Parameters.AddWithValue("@SLphieu", sp.SLphieu);
           cm.Parameters.AddWithValue("@GiaGoc", sp.GiaGoc);
           cm.Parameters.AddWithValue("@GiaBan", sp.GiaBan);
           cm.Parameters.AddWithValue("@ThoiGianBD", sp.ThoiGianBD);
           cm.Parameters.AddWithValue("@ThoiGianKT", sp.ThoiGianKT);
           cm.Parameters.AddWithValue("@DieuKien", sp.DieuKien);
           cm.Parameters.AddWithValue("@TrangThai", sp.TrangThai);
           cm.Parameters.AddWithValue("@MaLoai", sp.MaLoai);
           cm.Parameters.AddWithValue("@SLphieuban", sp.SLphieuban);
           cm.Parameters.AddWithValue("@Hinh", sp.Hinh);
           cm.ExecuteNonQuery();
       }
       public void Update(tbSanPham sp)
       {
           SqlCommand cm = new SqlCommand("tbSanPham_Update", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaSP", sp.MaSP);
           cm.Parameters.AddWithValue("@MaDN", sp.MaDN);
           cm.Parameters.AddWithValue("@TenSP", sp.TenSP);
           cm.Parameters.AddWithValue("@ChietKhau", sp.ChietKhau);
           cm.Parameters.AddWithValue("@ChiTietSP", sp.ChiTietSP);
           cm.Parameters.AddWithValue("@SLphieu", sp.SLphieu);
           cm.Parameters.AddWithValue("@GiaGoc", sp.GiaGoc);
           cm.Parameters.AddWithValue("@GiaBan", sp.GiaBan);
           cm.Parameters.AddWithValue("@ThoiGianBD", sp.ThoiGianBD);
           cm.Parameters.AddWithValue("@ThoiGianKT", sp.ThoiGianKT);
           cm.Parameters.AddWithValue("@DieuKien", sp.DieuKien);
           cm.Parameters.AddWithValue("@TrangThai", sp.TrangThai);
           cm.Parameters.AddWithValue("@MaLoai", sp.MaLoai);
           cm.Parameters.AddWithValue("@SLphieuban", sp.SLphieuban);
           cm.Parameters.AddWithValue("@Hinh", sp.Hinh);
           cm.ExecuteNonQuery();
       }
       public void Delete(int ID)
       {
           SqlCommand cm = new SqlCommand("tbSanPham_Detele", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaSP", ID);
           cm.ExecuteNonQuery();

       }
       public tbSanPhamCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("tbSanPham_GET", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Where", where);
           cm.Parameters.AddWithValue("@PageIndex", pageIndex);
           cm.Parameters.AddWithValue("@PageSize", pageSize);
           cm.Parameters.AddWithValue("@OrderBy", orderBy);
           cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
           cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
           SqlDataReader dr = cm.ExecuteReader();
           tbSanPhamCollection sanphamCollection = new tbSanPhamCollection();
           while (dr.Read())
           {
               tbSanPham Sanpham = new tbSanPham();
               Sanpham.MaSP = int.Parse(dr.GetValue(0).ToString());
               Sanpham.MaDN = int.Parse(dr.GetValue(1).ToString());
               Sanpham.TenSP = dr.GetValue(2).ToString();
               Sanpham.ChietKhau = int.Parse(dr.GetValue(3).ToString());
               Sanpham.ChiTietSP = dr.GetValue(4).ToString();
               Sanpham.SLphieu = int.Parse(dr.GetValue(5).ToString());
               Sanpham.GiaGoc = int.Parse(dr.GetValue(6).ToString());
               Sanpham.GiaBan = int.Parse(dr.GetValue(7).ToString());
               Sanpham.ThoiGianBD = DateTime.Parse(dr.GetValue(8).ToString());
               Sanpham.ThoiGianKT = DateTime.Parse(dr.GetValue(9).ToString());
               Sanpham.DieuKien = dr.GetValue(10).ToString();
               Sanpham.TrangThai = int.Parse(dr.GetValue(11).ToString());
               Sanpham.MaLoai = int.Parse(dr.GetValue(12).ToString());
               Sanpham.SLphieuban = int.Parse(dr.GetValue(13).ToString());
               Sanpham.Hinh = int.Parse(dr.GetValue(14).ToString());
               Sanpham.DiemDen = int.Parse(dr.GetValue(15).ToString());
               sanphamCollection.Add(Sanpham);
           }
           dr.Close();
           totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
           return sanphamCollection;


       }
    }
}
